using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Diagnostics;

namespace MeteorShower
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class MainMenuView : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        bool draw_green = false;
        bool draw_green_theme = false;
        bool change_theme = true; // controls changing the theme, stops it from changing over after button is released 
        Texture2D green, menuText, play, quit, line, menu, theme, background;
        Vector2 play_text, quit_text, menu_text, theme_text;
        SpriteFont font;

        int s_y = 480;
        int s_x = 840;

        public MainMenuView(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            Debug.WriteLine("MAIN MENU VIEW INITIALIZE");

            quit_text.X = 3 * s_x / 8;
            quit_text.Y = s_y / 2 + 50;

            menu_text.X = 6 * s_x / 16;
            menu_text.Y = 0;

            play_text.X = 2 * s_x / 8;
            play_text.Y = s_y / 2 + 50;

            theme_text.X = 4 * s_x / 8;
            theme_text.Y = s_y / 2 + 50;

            base.Initialize();
        }

        protected override void LoadContent()
        {
            Debug.WriteLine("MAIN MENU LOAD CONTENT");
            spriteBatch = new SpriteBatch(GraphicsDevice);

            menuText = Game.Content.Load<Texture2D>(Game1.getTheme().getTitleText());
            green = Game.Content.Load<Texture2D>("green");
            play = Game.Content.Load<Texture2D>(Game1.getTheme().getPlay());
            quit = Game.Content.Load<Texture2D>(Game1.getTheme().getQuit());
            line = Game.Content.Load<Texture2D>("line");
            menu = Game.Content.Load<Texture2D>(Game1.getTheme().getMenu());
            theme = Game.Content.Load<Texture2D>(Game1.getTheme().getTheme());
            background = Game.Content.Load<Texture2D>(Game1.getTheme().getBackground()); 
            font = Game.Content.Load<SpriteFont>("Segoe14");
        }

        public void reloadContent()
        {
            this.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            if (Game1.getGameState().Equals("mainmenuview"))
            {
                //background
                GraphicsDevice.Clear(Color.Black);
                
                spriteBatch.Begin();
                spriteBatch.Draw(background, new Vector2(0, 0), Color.White); 
                spriteBatch.Draw(menuText, new Vector2(s_x / 2, 3 * s_y / 8), null, Color.White, 0f, new Vector2(menuText.Width / 2, menuText.Height / 2), .9f, SpriteEffects.None, 0f);
                
                if (draw_green == true)
                {
                    spriteBatch.Draw(green, play_text, Color.White);
                }
                if (draw_green_theme == true)
                {
                    spriteBatch.Draw(green, theme_text, Color.White);
                }
                spriteBatch.Draw(play, play_text, Color.White);
                spriteBatch.Draw(quit, quit_text, Color.White);
                spriteBatch.Draw(theme, theme_text, Color.White); 
                spriteBatch.End();
                base.Draw(gameTime);
            }
        }




        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (Game1.getGameState().Equals("mainmenuview"))
            {
                var mouseState = Mouse.GetState();
                if (mouseState.LeftButton == ButtonState.Pressed)
                {
                    if (mouseState.X > play_text.X && mouseState.X < play_text.X + 100)
                    {
                        if (mouseState.Y > play_text.Y && mouseState.Y < play_text.Y + 50)
                        {
                            draw_green = true;
                        }
                        else
                        {
                            draw_green = false;
                        }
                    }
                }

                if (mouseState.LeftButton == ButtonState.Pressed)
                {
                    if (mouseState.X > theme_text.X && mouseState.X < theme_text.X + 100)
                    {
                        if (mouseState.Y > theme_text.Y && mouseState.Y < theme_text.Y + 50)
                        {
                            draw_green_theme = true;
                            change_theme = true; 
                        }
                        else
                        {
                            draw_green_theme = false;
                        }
                    }
                }

                //Handling back button being pressed
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                {
                    Debug.WriteLine("Should be exiting.....");
                    Game1.setGameState("exit");
                }

                // If play is selected

                if (mouseState.LeftButton == ButtonState.Released)
                {
                    if (mouseState.X > play_text.X && mouseState.X < play_text.X + 100)
                    {
                        if (mouseState.Y > play_text.Y && mouseState.Y < play_text.Y + 50)
                        {
                            Game1.setGameState("gameview");
                            Debug.WriteLine("GAME SHOULD RESTART");
                        }
                    }
                    draw_green = false;
                }

                if (mouseState.LeftButton == ButtonState.Released)
                {
                    if (mouseState.X > theme_text.X && mouseState.X < theme_text.X + 100)
                    {
                        if (change_theme == true)
                        {
                            if (mouseState.Y > theme_text.Y && mouseState.Y < theme_text.Y + 50)
                            {
                                Game1.changeTheme();
                                Debug.WriteLine("THEME SHOULD CHANGE");
                                change_theme = false;
                            }
                        }
                    }
                    draw_green_theme = false;
                }

                base.Update(gameTime);
            }
        }
    }
}
